/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;

namespace Xceed.Silverlight.Data.Stats
{
  public class CountFunction : CumulativeStatFunction
  {
    public CountFunction()
      : base()
    {
    }

    public CountFunction( string resultPropertyName, string sourcePropertyName )
      : base( resultPropertyName, sourcePropertyName )
    {
    }

    public CountFunction( string resultPropertyName, string sourcePropertyName, bool includeNullValues )
      : base( resultPropertyName, sourcePropertyName )
    {
      m_includeNullValues = includeNullValues;
    }

    #region IncludeNullValues Property

    public bool IncludeNullValues
    {
      get
      {
        return m_includeNullValues;
      }
      set
      {
        this.CheckSealed();
        m_includeNullValues = value;
      }
    }

    private bool m_includeNullValues = false;

    #endregion

    protected internal override void InitializeFromInstance( StatFunction source )
    {
      base.InitializeFromInstance( source );

      this.IncludeNullValues = ( ( CountFunction )source ).IncludeNullValues;
    }

    protected internal override void Validate()
    {
      base.Validate();
      this.ValidateSourcePropertyName( 1 );
    }

    protected internal override void Reset()
    {
      m_count = 0;
    }

    protected internal override void Accumulate( object[] values )
    {
      if( values.Length > 0 )
      {
        if( ( this.IncludeNullValues ) || ( ( values[ 0 ] != null ) && !( values[ 0 ] is DBNull ) ) )
        {
          checked
          {
            m_count++;
          }
        }
      }
    }

    protected internal override void AccumulateChildResult( StatResult childResult )
    {
      checked
      {
        m_count += Convert.ToInt64( childResult.Value );
      }
    }

    protected internal override StatResult GetResult()
    {
      return new StatResult( m_count );
    }

    protected internal override bool IsEquivalent( StatFunction statFunction )
    {
      CountFunction countFunction = statFunction as CountFunction;

      if( ( countFunction == null ) || ( countFunction.IncludeNullValues != this.IncludeNullValues ) )
        return false;

      return base.IsEquivalent( statFunction );
    }

    protected internal override int GetEquivalenceKey()
    {
      return base.GetEquivalenceKey() ^ this.IncludeNullValues.GetHashCode();
    }

    private long m_count;
  }
}
